#include "LineDisplayImp.h"
#include "LogUtil.h"
//using namespace Utility::LoggerLib;

namespace LineDisplayImp
{
	using namespace PortUtility;

	CLineDisplayImp::CLineDisplayImp()
	{
		m_portInfo.nParity = 2;
		m_portInfo.hCom = NULL;
		m_portInfo.oRead.hEvent = NULL;
		m_portInfo.oWrite.hEvent = NULL;
		m_nError = 0;
	}

	CLineDisplayImp::~CLineDisplayImp()
	{
		if (NULL != m_portInfo.hCom)
		{
			Close(); //Zhwang to do:
		}
	}

	int CLineDisplayImp::Open()
	{
		Utility::LoggerLib::Log(Utility::LoggerLib::DEBUG_LEVEL, Utility::LoggerLib::ELineDisplay, _T("Open the line display, port is COM%d."), m_portInfo.nPortIndex);
		if (m_portInfo.strCom.GetLength() == 0)
		{
			m_portInfo.hCom = OpenCom(m_portInfo.nPortIndex, m_portInfo.oWrite, m_portInfo.oRead, m_portInfo.nParity, m_nError);
		}
		else
		{
			m_portInfo.hCom = OpenCom(m_portInfo.strCom, m_portInfo.oWrite, m_portInfo.oRead, m_portInfo.nParity, m_nError);
		}
		

		if (m_nError != 0)
		{
	//		Log(ERROR_LEVEL, ELineDisplay, L"Fail to initialize line display, the error code is %d.", m_nError);
		}

		return m_nError;
	}

	int CLineDisplayImp::Close()
	{
	//	Log(DEBUG_LEVEL,ELineDisplay, _T("Close the line display, port is COM%d."), m_portInfo.nPort);
		if(PR_CloseCom(m_portInfo.hCom, m_portInfo.oWrite, m_portInfo.oRead))
		{
			m_portInfo.hCom = NULL;
			return 0;
		}

		return 1;
	}
}


